package com.lqq.sparrow.container;

import com.lqq.sparrow.Bootstrap;
import com.lqq.sparrow.container.config.SpringRemoteServiceConfig;
import com.lqq.sparrow.container.proxy.SpringProxyFactory;
import com.lqq.sparrow.invoke.provider.AbstractServer;
import com.lqq.sparrow.invoke.provider.Server;
import com.lqq.sparrow.proxy.ProxyInstance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author liangqq
 */
@Component
//@Configuration
@Slf4j
public class SpringBootstrap implements
        Bootstrap,
        InitializingBean,
        BeanFactoryPostProcessor,
        EnvironmentAware,
        ImportBeanDefinitionRegistrar {

    //@Autowired
    //private SpringProxyFactory springProxyFactory;

    @Autowired
    private SpringRemoteServiceConfig springRemoteServiceConfig;

    private Environment environment;

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //int port = springRemoteServiceConfig.getPort();
        String portStr = environment.getProperty("rpc.sparrow.remote-service.port");
        int port = Integer.parseInt(portStr);
        Server server = AbstractServer.getServer();
        server.start(port);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        //把远程服务的bean都注册到spring容器中

        //List<ProxyInstance> proxyInstanceList = springProxyFactory.getAllRemoteService();

        //这里生成bean的实例的方式不太合适, 后面优化?


        String classes = environment.getProperty("rpc.sparrow.remote-service.classes");

        List<ProxyInstance> proxyInstanceList = SpringProxyFactory.getAllRemoteService(classes);

        for(ProxyInstance proxyInstance: proxyInstanceList){

            beanFactory.registerSingleton(proxyInstance.getName(), proxyInstance.getInstance());
        }

        ;

    }


    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        log.info("{}", annotationMetadata);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;

    }
}
